﻿
// ———————————————————————–
// <copyright file="Tests.cs" company="The MITRE Corporation">
//    Copyright (c) 2014 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// Tests.cs:
//
// An abstract class for test files that includes some method interfaces and generic helper methods.
//
// Project: NIEMCADTest
// Author:  Lizzie DeYoung - MITRE
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2014 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)  
      

using EDXLSharp;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace NIEMCADTest
{
  /// <summary>
  /// The tests abstract class defines validation methods for bad and good read validation. For both, it
  /// takes as an argument the type of the data type being tested. It uses reflection on the type to 
  /// create an instance of this type and and call the ReadXML method. For bad XML, it expects that the
  /// read should fail due to an argument or validation exception. For good XML, it expects that the read
  /// should succeed.
  /// </summary>
  public abstract class Tests
  {
    protected delegate void _GOOD_READ_ValidationHelper(TestObject testObject);

    /// <summary>
    /// Instantiates and object of type <paramref name="type"/> and calls the ReadXML method
    /// on the object. If the read does not fail due to a <typeparamref name="ValidationException"/>
    /// or an <typeparamref name="ArgumentException"/>, the test fails.
    /// </summary>
    /// <param name="testObjects">The test objects containing the XML to test.</param>
    /// <param name="type">The type of object being tested.</param>
    protected void BAD_READ_Validation(List<TestObject> testObjects, Type type)
    {
      foreach (TestObject testObject in testObjects)
      {
        Debug.WriteLine(testObject.Xml.OuterXml);

        IComposable obj = (IComposable) Activator.CreateInstance(type, null);

        try
        {
          obj.ReadXML(testObject.Xml);
          Assert.Fail("Should have thrown a validation exception");
        }
        catch (ValidationException e)
        {
          Debug.WriteLine(e.Message);
        }
        catch (ArgumentException e)
        {
          Debug.WriteLine(e.Message);
        }
      }
    }

    /// <summary>
    /// Instantiates and object of type <paramref name="type"/> and calls the ReadXML method
    /// on the object. If the read fails for any reason, the test fails. Additionally, if it 
    /// exists, the method will call the <paramref name="helper"/> validation method to 
    /// perform further validation on the test object.
    /// </summary>
    /// <param name="testObjects">The test objects containing the XML to test.</param>
    /// <param name="type">The type of object being tested.</param>
    /// <param name="helper">A supplemental validation method.</param>
    protected void GOOD_READ_Validation(
      List<TestObject> testObjects, Type type, _GOOD_READ_ValidationHelper helper = null)
    {
      foreach (TestObject testObject in testObjects)
      {
        Debug.WriteLine(testObject.Xml.OuterXml);

        ComplexTypeBase obj = (ComplexTypeBase)Activator.CreateInstance(type, null);
        obj.ReadXML(testObject.Xml);
        testObject.Obj = obj;

        testObject.GOOD_READ_Validation();

        if (helper != null)
        {
          helper(testObject);
        }
      }
    }

    abstract public void BAD_READ_Validation(List<TestObject> testObjects);
    abstract public void GOOD_READ_Validation(List<TestObject> testObjects);
  }
}
